using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace minega
{
	public class KeyboardInfo
	{
		private KeyboardInfo() { }
		[DllImport("user32")]
		private static extern short GetKeyState(int vKey);
		public static KeyStateInfo GetKeyState(Keys key)
		{
			short keyState = GetKeyState((int)key);
			int low = Low(keyState);
			int high = High(keyState);
			bool toggled = low == 1 ? true : false;
			bool pressed = high == 1;
			return new KeyStateInfo(key, pressed, toggled);
		}
		private static int High(int keyState)
		{
			return keyState > 0 ? keyState >> 0x10
					: (keyState >> 0x10) & 0x1;
		}
		private static int Low(int keyState)
		{
			return keyState & 0xffff;
		}
	}


	public struct KeyStateInfo
	{
		private Keys _key;
		private bool _isPressed;
		private bool _isToggled;

		public KeyStateInfo(Keys key, bool ispressed, bool istoggled)
		{
			_key = key;
			_isPressed = ispressed;
			_isToggled = istoggled;
		}
		public static KeyStateInfo Default
		{
			get
			{
				return new KeyStateInfo(Keys.None, false, false);
			}
		}
		public Keys Key
		{
			get { return _key; }
		}
		public bool IsPressed
		{
			get { return _isPressed; }
		}
		public bool IsToggled
		{
			get { return _isToggled; }
		}
	}
}
